#include "scan.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

int scan_x;
int scan_y;

FILE *scan_f;

void scan_init(char *filename) {
  char line[100];
  char c;
  int  maxval, sizex, sizey;

  scan_f = fopen(filename,"r");

  fgets(line,100,scan_f);
  if ((line[0] != 'P') || (line[1] != '6')) {
    cerr << "*** Input is not a raw PPM file " << endl;
    exit(0);
  }
  while (c = fgetc(scan_f) == '#') { //--- comment
    fgets(line,100,scan_f);
  }
  fseek(scan_f, -1, SEEK_CUR);
  fscanf(scan_f,"%d", &scan_x);
  fscanf(scan_f,"%d", &scan_y);
  fscanf(scan_f,"%d", &maxval);
  c = fgetc(scan_f);

  cout << "*** INFO: PPM file " << filename << "  contains " << scan_x << " by " << scan_y << " pixels (max " << maxval << ")" << endl;
}

void scan_run(fifo<D_PIXEL> *R, fifo<D_PIXEL> *G, fifo<D_PIXEL> *B) {
  if (R->empty() && !feof(scan_f)) {
    R->write(getc(scan_f));
    G->write(getc(scan_f));
    B->write(getc(scan_f));
  }
}


